package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.enums.BacklogStatus;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DoubleUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysExpense;
import com.ruoyi.system.domain.SysExpenseStat;
import com.ruoyi.system.domain.SysTenantBacklog;
import com.ruoyi.system.mapper.SysTenantBacklogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysExpenseMergeMapper;
import com.ruoyi.system.domain.SysExpenseMerge;
import com.ruoyi.system.service.ISysExpenseMergeService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 收租费用合并Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-07-23
 */
@Service
public class SysExpenseMergeServiceImpl implements ISysExpenseMergeService
{
    @Autowired
    private SysExpenseMergeMapper sysExpenseMergeMapper;

    @Autowired
    private SysTenantBacklogMapper sysTenantBacklogMapper;

    /**
     * 查询收租费用合并
     * 
     * @param expenseMergeId 收租费用合并ID
     * @return 收租费用合并
     */
    @Override
    public SysExpenseMerge selectSysExpenseMergeById(Long expenseMergeId)
    {
        return sysExpenseMergeMapper.selectSysExpenseMergeById(expenseMergeId);
    }

    /**
     * 查询收租费用合并(上月水表电表度数)
     *
     * @param tenantIdStr 收租合同合并ID
     * @return 收租费用合并
     */
    @Override
    public SysExpenseMerge selectSysExpenseMergeByTenantId(String tenantIdStr) {
        return sysExpenseMergeMapper.selectSysExpenseMergeByTenantId(tenantIdStr);
    }

    /**
     * 查询收租费用合并列表
     * 
     * @param sysExpenseMerge 收租费用合并
     * @return 收租费用合并
     */
    @Override
    public List<SysExpenseMerge> selectSysExpenseMergeList(SysExpenseMerge sysExpenseMerge)
    {
        List<SysExpenseMerge> list = sysExpenseMergeMapper.selectSysExpenseMergeList(sysExpenseMerge);
        for (SysExpenseMerge expense : list) {
            expense = exchange(expense);
        }
        return list;
    }

    /**
     * 查询收租费用合并列表
     *
     * @param sysExpenseMerge 收租费用合并
     * @return 收租费用合并集合
     */
    @Override
    public SysExpenseMerge selectSysExpenseMergeSum(SysExpenseMerge sysExpenseMerge) {
        SysExpenseMerge expense = sysExpenseMergeMapper.selectSysExpenseMergeSum(sysExpenseMerge);
        expense = exchange(expense);
        return expense;
    }

    /**
     * 查询首页统计折线图应收收租金额（每栋）
     */
    @Override
    public List<SysExpenseMerge> selectSysExpenseMergeStatListForMonth() {
        return sysExpenseMergeMapper.selectSysExpenseMergeStatListForMonth();
    }
    /**
     * 查询首页统计折线图全部应收收租金额
     */

    @Override
    public List<SysExpenseMerge> selectSysExpenseMergeStatListForMonthAll() {
        return sysExpenseMergeMapper.selectSysExpenseMergeStatListForMonthAll();
    }


    /**
     * 查询首页统计折线图实收收租金额(每栋)
     */
    @Override
    public List<SysExpenseMerge> selectSysExpenseMergeStatListForMonthAlready() {
        return sysExpenseMergeMapper.selectSysExpenseMergeStatListForMonthAlready();
    }

    /**
     * 查询首页统计折线图实收收租金额(全部)
     */
    @Override
    public List<SysExpenseMerge> selectSysExpenseMergeStatListForMonthAlreadyAll() {
        return sysExpenseMergeMapper.selectSysExpenseMergeStatListForMonthAlreadyAll();
    }

    /**
     * 查询首页统计折线图十大租户(每个月)
     */
    @Override
    public List<SysExpenseMerge> selectSysExpenseMergeStatListForTenTenant() {
        return sysExpenseMergeMapper.selectSysExpenseMergeStatListForTenTenant();
    }

    /**
     * 查询本月已收租列表
     *
     * @param sysExpenseMerge 收租费用合并
     * @return 收租费用合并集合
     */
    @Override
    public List<SysExpenseMerge> selectSysExpenseMergeMonthList(SysExpenseMerge sysExpenseMerge) {
        return sysExpenseMergeMapper.selectSysExpenseMergeMonthList(sysExpenseMerge);
    }


    /**
     * 收租统计查询
     *
     * @param startTime,endTime 收租费用ID
     * @return 收租费用
     */
    @Override
    public List<SysExpenseStat> selectSysExpenseStat(String startTime, String endTime) {
        return sysExpenseMergeMapper.selectSysExpenseStat(startTime,endTime);
    }


    /**
     * 首页各栋已收金额统计
     *
     * @param
     * @return 收租费用
     */
    @Override
    public List<SysExpenseMerge> selectSysExpenseMergeStatList() {
        return sysExpenseMergeMapper.selectSysExpenseMergeStatList();
    }

    /**
     * 首页各栋已缴费户数
     *
     * @param
     */
    @Override
    public List<SysExpenseMerge> selectSysExpenseMergeCount() {
        return sysExpenseMergeMapper.selectSysExpenseMergeCount();
    }


    /**
     * 新增收租费用合并
     * 
     * @param sysExpenseMerge 收租费用合并
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysExpenseMerge(SysExpenseMerge sysExpenseMerge,String backlogId,String tenantIdStr)
    {
        String[] backlogArr = backlogId.split(",");
        for (String backlogIdTemp : backlogArr) {
            SysTenantBacklog sysTenantBacklog = new SysTenantBacklog();
            sysTenantBacklog.setBacklogId(Long.parseLong(backlogIdTemp));
            sysTenantBacklog.setBacklogStatus(BacklogStatus.COMPLETE.getCode());
            sysTenantBacklogMapper.updateSysTenantBacklog(sysTenantBacklog);
        }
        return sysExpenseMergeMapper.insertSysExpenseMerge(sysExpenseMerge);
    }

    /**
     * 修改收租费用合并
     * 
     * @param sysExpenseMerge 收租费用合并
     * @return 结果
     */
    @Override
    public int updateSysExpenseMerge(SysExpenseMerge sysExpenseMerge)
    {
        sysExpenseMerge.setUpdateTime(DateUtils.getNowDate());
        return sysExpenseMergeMapper.updateSysExpenseMerge(sysExpenseMerge);
    }

    /**
     * 删除收租费用合并对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysExpenseMergeByIds(String ids)
    {
        return sysExpenseMergeMapper.deleteSysExpenseMergeByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除收租费用合并信息
     * 
     * @param expenseMergeId 收租费用合并ID
     * @return 结果
     */
    @Override
    public int deleteSysExpenseMergeById(Long expenseMergeId)
    {
        return sysExpenseMergeMapper.deleteSysExpenseMergeById(expenseMergeId);
    }

    /**
     * 小数点转换
     *
     * @return 结果
     */
    @Override
    public SysExpenseMerge exchange(SysExpenseMerge expense){
        if(StringUtils.isNotBlank(expense.getMonthlyRentFee())){ expense.setMonthlyRentFee(String.valueOf(DoubleUtil.round(Double.parseDouble(expense.getMonthlyRentFee()),2))); }
        if(StringUtils.isNotBlank(expense.getRealMonthlyRentFee())){expense.setRealMonthlyRentFee(String.valueOf(DoubleUtil.round(Double.parseDouble(expense.getRealMonthlyRentFee()),2)));}
        if(StringUtils.isNotBlank(expense.getPropertyFee())){expense.setPropertyFee(String.valueOf(DoubleUtil.round(Double.parseDouble(expense.getPropertyFee()),2)));}
        if(StringUtils.isNotBlank(expense.getRealPropertyFee())){expense.setRealPropertyFee(String.valueOf(DoubleUtil.round(Double.parseDouble(expense.getRealPropertyFee()),2)));}
        if(StringUtils.isNotBlank(expense.getTotalUtilityFee())){expense.setTotalUtilityFee(String.valueOf(DoubleUtil.round(Double.parseDouble(expense.getTotalUtilityFee()),2)));}
        if(StringUtils.isNotBlank(expense.getWlanFee())){expense.setWlanFee(String.valueOf(DoubleUtil.round(Double.parseDouble(expense.getWlanFee()),2)));}
        if(StringUtils.isNotBlank(expense.getTotalMonthRealFee())){expense.setTotalMonthRealFee(String.valueOf(DoubleUtil.round(Double.parseDouble(expense.getTotalMonthRealFee()),2)));}
        if(StringUtils.isNotBlank(expense.getApportionRatio())){expense.setApportionRatio(String.valueOf(DoubleUtil.round(Double.parseDouble(expense.getApportionRatio()),4)));}
        if(StringUtils.isNotBlank(expense.getNinetyPercentRealMonthlyFee())){expense.setNinetyPercentRealMonthlyFee(String.valueOf(DoubleUtil.round(Double.parseDouble(expense.getNinetyPercentRealMonthlyFee()),2)));}
        if(StringUtils.isNotBlank(expense.getUseElectricFee())){expense.setUseElectricFee(String.valueOf(DoubleUtil.round(Double.parseDouble(expense.getUseElectricFee()),2)));}
        if(StringUtils.isNotBlank(expense.getApportionElectricFee())){expense.setApportionElectricFee(String.valueOf(DoubleUtil.round(Double.parseDouble(expense.getApportionElectricFee()),2)));}
        if(StringUtils.isNotBlank(expense.getUseWaterFee())){expense.setUseWaterFee(String.valueOf(DoubleUtil.round(Double.parseDouble(expense.getUseWaterFee()),2)));}
        if(StringUtils.isNotBlank(expense.getApportionWaterFee())){expense.setApportionWaterFee(String.valueOf(DoubleUtil.round(Double.parseDouble(expense.getApportionWaterFee()),2)));}
        if(StringUtils.isNotBlank(expense.getUseTotalUtilityFee())){expense.setUseTotalUtilityFee(String.valueOf(DoubleUtil.round(Double.parseDouble(expense.getUseTotalUtilityFee()),2)));}
        if(StringUtils.isNotBlank(expense.getApportionTotalUtilityFee())){expense.setApportionTotalUtilityFee(String.valueOf(DoubleUtil.round(Double.parseDouble(expense.getApportionTotalUtilityFee()),2)));}
        return expense;
    }
}
